<template>
    <zh-form ref="formRef" v-loading="data.loading" :model="data.formData" label-width="100px" :span="props.downloadFlag?12:24" :rules="props.downloadFlag?data.downRules:data.rules">
      <zh-form-input
          prop="projectName"
          label="项目名"
          :disabled="props.disabled"
          v-model="data.formData.projectName"
      ></zh-form-input>
      <zh-form-input
          prop="projectCode"
          label="项目标识"
          :disabled="props.disabled"
          v-model="data.formData.projectCode"
      ></zh-form-input>
      <zh-form-input
          prop="projectPackage"
          label="项目包名"
          :disabled="props.disabled"
          v-model="data.formData.projectPackage"
      ></zh-form-input>
      <zh-form-input
          prop="projectPath"
          label="项目路径"
          :disabled="props.disabled"
          v-model="data.formData.projectPath"
      ></zh-form-input>
      <zh-col v-if="props.downloadFlag" :span="24">
        <el-divider> 变更后的信息 </el-divider>
      </zh-col>
      <zh-form-input
          prop="modifyProjectName"
          label="项目名"
          :span="24"
          v-if="props.downloadFlag"
          v-model="data.formData.modifyProjectName"
      ></zh-form-input>
      <zh-form-input
          prop="modifyProjectCode"
          label="项目标识"
          v-if="props.downloadFlag"
          v-model="data.formData.modifyProjectCode"
      ></zh-form-input>
      <zh-form-input
          prop="modifyProjectPackage"
          label="项目包名"
          v-if="props.downloadFlag"
          v-model="data.formData.modifyProjectPackage"
      ></zh-form-input>
      <zh-form-input
          prop="exclusions"
          label="排除文件"
          type="textarea"
          v-if="props.downloadFlag"
          v-model="data.formData.exclusions"
      ></zh-form-input>
      <zh-form-input
          prop="modifySuffix"
          label="变更文件"
          type="textarea"
          v-if="props.downloadFlag"
          v-model="data.formData.modifySuffix"
      ></zh-form-input>
    </zh-form>
</template>

<script lang="ts" setup name="genProjectModifyForm">
import { ElMessage } from 'element-plus';

//------------------定义子组件---------------------------
const formRef = ref();
//------------------接收传入参数(组件)-------------------
const props = defineProps({
  id: {
    type: String,
    default: '',
  },
  disabled: {
    type: Boolean,
    default: true,
  },
  downloadFlag: {
    type: Boolean,
    default: false,
  },
  btnLoding: Boolean,
});
const emits = defineEmits(['update:btnLoding', 'canner', 'refreshTable']);
//------------------初始化------------------------------
onMounted(() => {
  getDetail();
});
//------------------内部变量定义-------------------------
const data = reactive({
  formData: {exclusions:'.git,.idea,target,logs',modifySuffix:'java,xml,yml,txt'} as any,
  detailVisible: true,
  loading: false,
  rules: {
    projectName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    projectCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    projectPackage: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    projectPath: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
  },
  downRules:{
    projectName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    projectCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    projectPackage: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    projectPath: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    modifyProjectName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    modifyProjectCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    modifyProjectPackage: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    exclusions: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    modifySuffix: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
  },
  dictData: {} as any,
  areaCodeDict: [] as any,
  campusParam: {} as any,
});

// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = '';
  $api.common.getBatchDict.post(params).then((res) => {
    data.dictData = res.result;
  });
};

//获取详情
const getDetail = () => {
  if (!props.id) {
    return;
  }
  data.loading = true;
  $api.common.send
  .post('genProjectModify0002', { id: props.id })
  .then((res: any) => {
    let result=res.result || {};
    if(!result.exclusions){
      result.exclusions='.git,.idea,target,logs'
    }
    if(!result.modifySuffix){
      result.modifySuffix='java,xml,yml,txt'
    }
    data.formData =result
    data.loading = false;
    data.campusParam.schoolId = data.formData.schoolId;
  })
  .catch(() => {
    data.loading = false;
  });
};

//保存
async function save() {
  let checkFlag = await formRef.value.checkForm();
  if (checkFlag) {
    emits('update:btnLoding', true);
    if(props.downloadFlag){
      //下载文件
      $api.common.download.post("genProjectModify0007",data.formData,data.formData.modifyProjectName+'.zip');
    }else{
      if (data.formData.id) {
        $api.common.send
        .post('genProjectModify0005', data.formData)
        .then(() => {
          ElMessage.success('修改成功');
          emits('update:btnLoding', false);
          emits('canner', (data.detailVisible = false));
          emits('refreshTable');
        })
        .catch(() => {
          emits('update:btnLoding', false);
          emits('canner', (data.detailVisible = true));
        });
      } else {
        $api.common.send
        .post('genProjectModify0004', data.formData)
        .then(() => {
          ElMessage.success('新增成功');
          emits('update:btnLoding', false);
          emits('canner', (data.detailVisible = false));
          emits('refreshTable');
        })
        .catch(() => {
          emits('update:btnLoding', false);
          emits('canner', (data.detailVisible = true));
        });
      }
    }
    
  }
}

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ save });
</script>
<style lang="scss" scoped></style>
